package com.xu.fountain.Controller;

import com.xu.fountain.Entity.UserEntity;
import com.xu.fountain.Repository.UserRepository;
import com.xu.fountain.Util.MD5Util;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Message: created by XUBO on 2018/4/12
 * @Description:
 */

@RequestMapping(value="user")
@RestController
public class UserController {

    Logger LOG = Logger.getLogger(UserController.class);

    @Autowired
    private UserRepository userRepos;

    @RequestMapping(value="saveUser")
    public UserEntity saveUser(UserEntity user){
        for (int i=1;i<400;i++){
            user.setId( i );
            user.setUsername( "xubo"+i );
            user.setPassword(MD5Util.encode("xubo"+i) );
            userRepos.save(user);
        }
        user.setUsername( "xubo101" );
        return userRepos.save(user);
    }

    @RequestMapping("findAll")
    @ResponseBody
    public Page<UserEntity> findAll(UserEntity user, int page, int size){
        Example<UserEntity> ex = Example.of(user);
        return  userRepos.findAll( ex, new  PageRequest(page-1, size));
    }

    @RequestMapping("/test")
    @ResponseBody
    public Page<UserEntity> test(int page,int size){
        System.out.println( "page:"+page+"size:"+size );
        return userRepos.findAll( new  PageRequest(page, size));
    }

}